package cn.ekuma.epos.crm.link;

import cn.ekuma.epos.crm.model.CustomerStoreValueCardDiaryTableModel;
import cn.ekuma.epos.datalogic.I_DataLogicSales;
import cn.ekuma.epos.db.table.crm.I_CustomerStoreValueCardDiary;
import com.openbravo.bean.crm.CustomerStoreValueCard;
import com.openbravo.bean.crm.CustomerStoreValueCardDiary;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.gui.MessageInf;
import com.openbravo.data.loader.query.QBF;
import com.openbravo.data.loader.query.QBFCompareEnum;

import com.openbravo.data.loader.query.QBFParameter;
import com.openbravo.pos.base.AppLocal;
import com.openbravo.pos.panels.event.TimeCycleChangedEvent;

public class JCustomerStoreValueCardDiaryLinkPanel extends AbstractCustomerStoreValueCardLinkTablePanel<CustomerStoreValueCardDiary>{

	Object[] afilter;
	public JCustomerStoreValueCardDiaryLinkPanel(I_DataLogicSales dlSales) {
		super(dlSales, new CustomerStoreValueCardDiaryTableModel());
	}

	@Override
	public void timeCycleChanged(TimeCycleChangedEvent e) {
		if(afilter==null)
			return;
		  afilter[0] = (e.getStartDate()== null)? QBFCompareEnum.COMP_NONE: QBFCompareEnum.COMP_GREATEROREQUALS;
	      afilter[1] = e.getStartDate();
	      afilter[2] = ( e.getEndDate() == null) ? QBFCompareEnum.COMP_NONE: QBFCompareEnum.COMP_LESS;
	      afilter[3] = e.getEndDate();
	      query();
	}
	
	@Override
	public void baseBeanChanged(CustomerStoreValueCard o) {
		if(afilter==null)
			try {
				afilter=(Object[]) createValue();
			} catch (BasicException e) {
				e.printStackTrace();
			}
	   afilter[4] = QBFCompareEnum.COMP_EQUALS;
       afilter[5] = o.getId();
       query();
	}

	@Override
	public Object createValue() throws BasicException {
		Object[] obj=new Object[6];
		obj[0] = (timerFilter.getStartDate()== null)? QBFCompareEnum.COMP_NONE: QBFCompareEnum.COMP_GREATEROREQUALS;
		obj[1] = timerFilter.getStartDate();
		obj[2] = ( timerFilter.getEndDate() == null) ? QBFCompareEnum.COMP_NONE: QBFCompareEnum.COMP_LESS;
		obj[3] = timerFilter.getEndDate();
		obj[4] = QBFCompareEnum.COMP_NONE;
		obj[5] = null;
	    return obj;
	}
	
	String[] fields=new String[]{I_CustomerStoreValueCardDiary.CURDATE, I_CustomerStoreValueCardDiary.CURDATE, I_CustomerStoreValueCardDiary.CARDID};
	public void query(){
		if(afilter==null)
			return;
		QBF qbf=QBF.c();
		for(int i=0;i<afilter.length;i=i+2)
			if(afilter[i]!=QBFCompareEnum.COMP_NONE)
				qbf.and(new QBFParameter(fields[i/2],(QBFCompareEnum) afilter[i],afilter[i+1]));
		try {
			tableModel.resert(dlSales.query(CustomerStoreValueCardDiary.class,qbf.build()));
		} catch (BasicException e) {
			new MessageInf(e).show(this);
		}
	}
	
	public static String getTabName(){
		return AppLocal.getIntString("label.CustomerStoreValueCardDiary");
	}

}
